package com.ht.api.call.func.impl.fan;

import cn.hutool.json.JSONObject;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import com.ht.api.db.exception.HtException;

/**
 * 渠道广告位的查询后置函数
 */
public class PositionChannelQueryAfterFunctionCall implements IFunctionCall{

	@Override
	public String getName() {
		return "渠道广告位的查询后置函数";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {

		DbList list = result.getRecords();

		for (Entity position : list) {
			String inType = position.getStr("in_type");

			String url = position.getStr("url");
			if (inType.equals("wx_official_customer")) {
				position.set("url", url);
				continue;
			}

			Entity config = Db.createSqlParser(inType).where("position_id", position.getMainId()).findOne(me.getDataSource());

			if (config == null) {
				continue;
			}

			if (inType.equals("wx_customer_link")) {
				url = config.getStr("url") + "?customer_channel=" + config.getStr("state");
				int error = config.getInt("error");
				position.set("error", error);
			} else if (inType.equals("wx_chat_way")) {
				url = config.getStr("qr_code");
			} else if (inType.equals("wx_contact_way")) {
				url = config.getStr("qr_code");
				position.set("qr_code_chat", config.get("qr_code_chat"));
			} else if (inType.equals("kf_account")) {
				url = config.getStr("url") + "&scene_param=" + position.getStr("state");
			}

			position.set("url", url);
		}


		return null;
	}

}
